﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Zor_Web.Models.Sincerity;
namespace Zor_Web.Controllers
{
    public class SincerityController : Controller
    {
        /// <summary>
        /// 诚信信息登录
        /// </summary>
        /// <returns></returns>

        #region - 房屋信息审核控制器 -
        public ActionResult PropertyInfo(V_PropertyInfo Model, int? index = 1, string where = null, string shzt = "")
        {

            int id = Zor_Tools.RequestSession.GetSessionUser().User_Id;
            Zor_Modes.Api_Users user = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByUserID(id.ToString());


            Dictionary<string, string> parm = new Dictionary<string, string>();

            int isAdmin = Zor_Tools.RequestSession.GetSessionUser().Is_Admin;

            if (isAdmin == 1 || user.Dt_Level == 1)
            {
                parm.Add("Dt_ParentId", "");
                parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 2)
            {
                Zor_Modes.Api_Users tUser = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByParentID(id.ToString());
                parm.Add("Dt_ParentId", tUser.DT_Id.ToString());
                //parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 3)
            {
                parm.Add("Dt_ParentId", user.DT_Id.ToString());
                //parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 4)
            {
                //parm.Add("Dt_ParentId", "");
                parm.Add("Dt_Id", user.DT_Id.ToString());
            }

            string strLst = Zor_API.PropertyInfo.GetPropertyInfoList(parm);

            List<V_PropertyInfo> lst = Zor_Tools.JsonHelper.JonsToList<V_PropertyInfo>(strLst);

            if (where != null)
                where = where.Trim();
            if (!string.IsNullOrEmpty(where))
            {
                lst = lst.Where(m =>
                {
                    return (
                        m.LessorName.IndexOf(where) >= 0 ||
                        m.LessorIDCardNo.IndexOf(where) >= 0 ||
                        m.LessorCell.IndexOf(where) >= 0 ||

                        m.LesseeName.IndexOf(where) >= 0 ||
                        m.LesseeIDCardNo.IndexOf(where) >= 0 ||
                        m.LesseeCell.IndexOf(where) >= 0 ||

                        m.UserName.IndexOf(where) >= 0 ||
                        m.UserCardId.IndexOf(where) >= 0 ||
                        m.UserCell.IndexOf(where) >= 0
                        );
                }).ToList<V_PropertyInfo>();
            }
            if (!string.IsNullOrEmpty(shzt))
            {
                lst = lst.Where(m => { return m.AuditStatus == int.Parse(shzt); }).ToList<V_PropertyInfo>();

            }
            TempData["where"] = where;
            TempData["shzt"] = shzt;
            lst = lst.OrderBy(m => { return m.AuditStatus; }).ToList<V_PropertyInfo>();
            int idx = index.HasValue ? index.Value : 1;
            PagedList<V_PropertyInfo> pl = new PagedList<V_PropertyInfo>(lst, idx, 10);
            Zor_Web.Models.Sincerity.V_PropertyInfo vp = new V_PropertyInfo();

            vp.PageData = pl;

            return View(vp);

        }
        #endregion

        #region - 诚信档案控制器 -
        public ActionResult Sincerity(V_Sincerity Model, int? index = 1, string where = null)
        {
            int id = Zor_Tools.RequestSession.GetSessionUser().User_Id;
            Zor_Modes.Api_Users user = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByUserID(id.ToString());


            Dictionary<string, string> parm = new Dictionary<string, string>();

            int isAdmin = Zor_Tools.RequestSession.GetSessionUser().Is_Admin;

            if (isAdmin == 1 || user.Dt_Level == 1)
            {
                parm.Add("Dt_ParentId", "");
                parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 2)
            {
                Zor_Modes.Api_Users tUser = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByParentID(id.ToString());
                parm.Add("Dt_ParentId", tUser.DT_Id.ToString());
                //parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 3)
            {
                parm.Add("Dt_ParentId", user.DT_Id.ToString());
                //parm.Add("Dt_Id", "");
            }
            else if (user.Dt_Level == 4)
            {
                //parm.Add("Dt_ParentId", "");
                parm.Add("Dt_Id", user.DT_Id.ToString());
            }

            string strLst = Zor_API.PropertyInfo.GetSincerityInfoList(parm);

            List<V_Sincerity> lst = Zor_Tools.JsonHelper.JonsToList<V_Sincerity>(strLst);
            if (where != null)
                where = where.Trim();
            if (!string.IsNullOrEmpty(where))
            {
                lst = lst.Where(m =>
                {
                    return (
                        m.LessorName.IndexOf(where) >= 0 ||
                        m.LessorIDCardNo.IndexOf(where) >= 0 ||
                        m.LessorCell.IndexOf(where) >= 0 ||

                        m.LesseeName.IndexOf(where) >= 0 ||
                        m.LesseeIDCardNo.IndexOf(where) >= 0 ||
                        m.LesseeCell.IndexOf(where) >= 0 ||

                        m.UserName.IndexOf(where) >= 0 ||
                        m.UserCardId.IndexOf(where) >= 0 ||
                        m.UserCell.IndexOf(where) >= 0
                        );
                }).ToList<V_Sincerity>();
            }


            TempData["where"] = where;


            int idx = index.HasValue ? index.Value : 1;
            PagedList<V_Sincerity> pl = null;

            pl = new PagedList<V_Sincerity>(lst, idx, 10);

            V_Sincerity vp = new V_Sincerity();
            vp.PageData = pl;

            return View(vp);

        }

        #endregion

        #region - 诚信统计 -

        static string strNames = "";// V_SincerityTotal vTotal = new V_SincerityTotal();
        public ActionResult GetNames()
        {
            return Content(strNames);

        }
        static string strCnts = "";
        public ActionResult GetCnts()
        {
            return Content(strCnts);
        }
        public ActionResult SincerityTotal(string startTime, string endTime)
        {
            if (string.IsNullOrEmpty(startTime))
                startTime = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            if (string.IsNullOrEmpty(endTime))
                endTime = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            TempData["startTime"] = startTime;
            TempData["endTime"] = endTime;
            Dictionary<string, string> parm = new Dictionary<string, string>();
            parm.Add("startTime", startTime);
            parm.Add("endTime", endTime);



            int id = Zor_Tools.RequestSession.GetSessionUser().User_Id;
            Zor_Modes.Api_Users user = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByUserID(id.ToString());


            int isAdmin = Zor_Tools.RequestSession.GetSessionUser().Is_Admin;

            if (isAdmin == 1 || user.Dt_Level == 1)
            {
                parm.Add("Dt_ParentId", "0");
                parm.Add("Dt_Id", "0");
            }
            else if (user.Dt_Level == 2)
            {
                Zor_Modes.Api_Users tUser = Zor_Bll.Sincerity_Bll<Zor_Modes.Api_Users>.GetApiUserByParentID(id.ToString());
                parm.Add("Dt_ParentId", tUser.DT_Id.ToString());
                parm.Add("Dt_Id", "0");
            }
            else if (user.Dt_Level == 3)
            {
                parm.Add("Dt_ParentId", user.DT_Id.ToString());
                parm.Add("Dt_Id", "0");
            }
            else if (user.Dt_Level == 4)
            {
                parm.Add("Dt_ParentId", "0");
                parm.Add("Dt_Id", user.DT_Id.ToString());
            }
            string strLst = Zor_API.PropertyInfo.GetPropertyInfoStatisticsByYear(parm);
            List<V_SincerityTotal> lst = Zor_Tools.JsonHelper.JonsToList<V_SincerityTotal>(strLst);
            V_SincerityTotal vt = new V_SincerityTotal();
            StringBuilder sbCnt = new StringBuilder();
            StringBuilder sbNian = new StringBuilder();
            StringBuilder sbName = new StringBuilder();
            sbCnt.Append("[");
            sbNian.Append("[");
            sbName.Append("[");
            if (lst != null)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    sbCnt.Append("" + lst[i].Cnt + "");
                    sbNian.Append("'" + lst[i].Nian + "'");
                    sbName.Append("'" + lst[i].Name + "'");
                    if (i < lst.Count - 1)
                    {
                        sbCnt.Append(",");
                        sbNian.Append(",");
                        sbName.Append(",");
                    }
                }
            }
            sbCnt.Append("]");
            sbNian.Append("]");
            sbName.Append("]");
            vt.CntArr = sbCnt.ToString();
            vt.NameArr = sbName.ToString();
            vt.NianArr = sbNian.ToString();
            strNames = sbName.ToString();
            strCnts = sbCnt.ToString();
            //TempData["myCnt"] = sbCnt.ToString();
            //TempData["myNian"] = sbNian.ToString();
            //TempData["myName"] = sbName.ToString();

            return View(vt);
        }
        #endregion
    }
}
